﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

namespace WebApplication1
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        string empid;
        private OleDbConnection accessconn;
        protected void Page_Load(object sender, EventArgs e)
        {
            accessconn = new OleDbConnection();
            accessconn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                                @"C:\Dbase_SEA\myDB.accdb";
            TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + Okay.UniqueID + "').click();return false;}} else {return true}; ");
            TextBox2.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + Okay.UniqueID + "').click();return false;}} else {return true}; ");
            rows.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + Okay.UniqueID + "').click();return false;}} else {return true}; ");
            if (Page.IsPostBack)
            {
                if (stat == false && TextBox1.Visible == true)
                {
                    Okay_Click1();
                }
            }
            this.Label1.Text = Request.QueryString["empname"];
            empid = Request.QueryString["eid"];
            Label2.Visible = false;
            Label3.Visible = false;
            Label4.Visible = false;
            TextBox1.Visible = false;
            TextBox2.Visible = false;
            TextBox3.Visible = false;
            total.Visible = false;
            rows.Visible = false;
            Okay.Visible = false;
        }
        bool stat = false;
        //Menu Order Click 
        protected void Button3_Click(object sender, EventArgs e)
        {
            Label2.Visible = true;
            Label3.Visible = true;
            Label4.Visible = true;
            TextBox1.Visible = true;
            TextBox2.Visible = true;
            rows.Visible = true;
            Okay.Visible = true;
            TextBox1.Text = "";
            TextBox2.Text = "";
            rows.Text = "";
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            Response.Redirect("http://localhost:62505/Default.aspx");    
        }
        //Use the function to validate the value in the Table id, Customers and Number of items 
        public bool validatenum(string number)
        {
            bool ichar = true;
            for (int i = 0; i < number.Length; i++)
            {
                if (!char.IsNumber(number[i]))
                {
                    ichar = false;
                }
            }
            return ichar;
        }
        
        Table tbl = new Table();
        DataSet ds;
        int tblRows=0;
        //click of the button to create dynamic controls
        protected void Okay_Click(object sender, EventArgs e)
        {
            Label2.Visible = true;
            Label3.Visible = true;
            Label4.Visible = true;
            TextBox1.Visible = true;
            TextBox2.Visible = true;
            rows.Visible = true;
            Okay.Visible = true;
            if (stat == true)
            {
                 Okay_Click1();           
            }
        }
        private void Okay_Click1()
        {
            Label2.Visible = true;
            Label3.Visible = true;
            Label4.Visible = true;
            TextBox1.Visible = true;
            TextBox2.Visible = true;
            rows.Visible = true;
            
            PlaceHolder1.Controls.Clear();
            if(validatenum(TextBox1.Text) == true && validatenum(TextBox2.Text) == true && TextBox1.Text != "" && TextBox2.Text != "")
            {
                accessconn.Open();
                ds = new DataSet();
                string query2 = "select ItemEntry, ItemName, Cost from FoodItem";
                OleDbDataAdapter da = new OleDbDataAdapter(query2, accessconn);
                da.Fill(ds);
                int x = Convert.ToInt32(TextBox1.Text);
                int y = Convert.ToInt32(TextBox2.Text);
                //create table Dynamically
                if (x <= 20 && x >= 1)
                {
                    if (y <= 30 && y >= 0)
                    {
                        if (rows.Text != "" && validatenum(rows.Text) == true)
                        {
                            tblRows = Convert.ToInt32(rows.Text);

                            //tbl = new Table();
                            PlaceHolder1.Controls.Add(tbl);
                            TableRow tr1 = new TableRow();
                            TableCell col1 = new TableCell();
                            col1.Width = 50;
                            Label itemno1 = new Label();
                            itemno1.Text = "Item No";
                            itemno1.EnableViewState = false;
                            itemno1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                            col1.Controls.Add(itemno1);
                            tr1.Cells.Add(col1);
                            TableCell col2 = new TableCell();
                            col2.Width = 250;
                            Label itemname1 = new Label();
                            itemname1.Text = "Item Name";
                            itemname1.EnableViewState = false;
                            itemname1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                            col2.Controls.Add(itemname1);
                            tr1.Cells.Add(col2);
                            TableCell col3 = new TableCell();
                            col3.Width = 150;
                            Label qty1 = new Label();
                            qty1.Text = "Quantity";
                            qty1.EnableViewState = false;
                            qty1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                            col3.Controls.Add(qty1);
                            tr1.Cells.Add(col3);
                            TableCell col4 = new TableCell();
                            col4.Width = 150;
                            Label price1 = new Label();
                            price1.Text = "Price";
                            price1.EnableViewState = false;
                            price1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                            col4.Controls.Add(price1);
                            tr1.Cells.Add(col4);
                            TableCell col5 = new TableCell();
                            col5.Width = 150;
                            Label totprice1 = new Label();
                            totprice1.Text = "Total Price";
                            totprice1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#66FF33");
                            col5.Controls.Add(totprice1);
                            tr1.Cells.Add(col5);

                            tbl.Rows.Add(tr1);

                            // Now iterate through the table and add your controls 
                            for (int i = 0; i < tblRows; i++)
                            {
                                TableRow tr = new TableRow();
                                TableCell tc1 = new TableCell();
                                tc1.Width = 50;
                                TextBox itemno = new TextBox();
                                itemno.Text = Convert.ToString(i + 1) + ".";
                                itemno.ID = "itemno" + Convert.ToString(i + 1);
                                tc1.Controls.Add(itemno);
                                tr.Cells.Add(tc1);
                                TableCell tc2 = new TableCell();
                                tc2.Width = 250;
                                DropDownList itemname = new DropDownList();
                                itemname.ID = "itemname" + Convert.ToString(i + 1);
                                itemname.AutoPostBack = true;
                                itemname.SelectedIndexChanged += new EventHandler(itemname_SelectedIndexChanged);
                                itemname.Width = 250;
                                itemname.DataSource = ds.Tables[0]; //gets the food items from the database
                                itemname.DataTextField = ds.Tables[0].Columns["ItemName"].ColumnName.ToString();
                                itemname.DataValueField = ds.Tables[0].Columns["ItemEntry"].ColumnName.ToString();
                                itemname.DataBind();
                                ListItem item1 = new ListItem();
                                item1.Text = "--Select--";
                                itemname.Items.Insert(0, item1);
                                tc2.Controls.Add(itemname);
                                tr.Cells.Add(tc2);
                                TableCell tc3 = new TableCell();
                                tc3.Width = 150;
                                TextBox qty = new TextBox();
                                qty.ID = "qty" + Convert.ToString(i + 1);
                                tc3.Controls.Add(qty);
                                tr.Cells.Add(tc3);
                                TableCell tc4 = new TableCell();
                                tc4.Width = 150;
                                TextBox price = new TextBox();
                                price.ID = "price" + Convert.ToString(i + 1);
                                tc4.Controls.Add(price);
                                tr.Cells.Add(tc4);
                                TableCell tc5 = new TableCell();
                                tc5.Width = 150;
                                TextBox totprice = new TextBox();
                                totprice.ID = "totprice" + Convert.ToString(i + 1);
                                tc5.Controls.Add(totprice);
                                tr.Cells.Add(tc5);

                                tbl.Rows.Add(tr);
                            }
                        }
                        else MessageBox("Please enter the number of Items");
                    }
                    else MessageBox("Customer Count is not within range..!");
                }
                else MessageBox("Invalid Table Number");
                accessconn.Close();
            }
            else MessageBox("Please Enter a Number");
        }
        // update price for the item selected from the dropdown list
        public void itemname_SelectedIndexChanged(Object sender, EventArgs e)
        {
            stat = false;
            Label2.Visible = true;
            Label3.Visible = true;
            Label4.Visible = true;
            TextBox1.Visible = true;
            TextBox2.Visible = true;
            rows.Visible = true;
            total.Visible = true;
            TextBox3.Visible = true;
            for (int i = 1; i <= tblRows; i++)
            {
                string s = ((DropDownList)Page.FindControl("itemname"+i)).SelectedItem.Text;
                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                {
                    if (s == ds.Tables[0].Rows[j]["ItemName"].ToString())
                    {
                      ((TextBox)Page.FindControl("price"+i)).Text = ds.Tables[0].Rows[j]["Cost"].ToString();             
                    }
                }
            }

        }
        
        //calculate the total price( price * quantity) and total amount
        //And also adds the information to the database
        public void total_Click(object sender, EventArgs e)
        {
            string[] arr = new string[100];
            stat = false;
            Label2.Visible = true;
            Label3.Visible = true;
            Label4.Visible = true;
            TextBox1.Visible = true;
            TextBox2.Visible = true;
            rows.Visible = true;
            total.Visible = true;
            TextBox3.Visible = true;
            double z = 0.0;
            if (((TextBox)Page.FindControl("qty1")).Text != "")
            {
                for (int i = 1; i <= tblRows; i++)
                {
                    if (((TextBox)Page.FindControl("qty" + i)).Text != "")
                    {
                        double x = Convert.ToDouble(((TextBox)Page.FindControl("qty" + i)).Text);
                        double y = Convert.ToDouble(((TextBox)Page.FindControl("price" + i)).Text);
                        ((TextBox)Page.FindControl("totprice" + i)).Text = Convert.ToString(x * y);
                        z += x * y;
                    }
                }
                TextBox3.Text = Convert.ToString(z);
                accessconn.Open();
                string str1 = TextBox1.Text;
                string str2 = TextBox2.Text;
                string str3 = TextBox3.Text;
                string str4 = DateTime.Now.ToShortDateString();
                //Inserts order information to foodorder table
                string query3 = "INSERT INTO FoodOrder([Tableid], [Eid], [OrderDate], [CustomerCount], [Amount]) VALUES ('" + str1 + "','" + empid + "','" + str4 + "','" + str2 + "','" + str3 + "') ";
                OleDbCommand comm = new OleDbCommand(query3, accessconn);
                comm.ExecuteNonQuery();
                //gets the respective order id from the database
                string query4 = "select MAX(Orderid) as oid from FoodOrder";
                DataSet ds1 = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(query4, accessconn);
                da.Fill(ds1);
                int orderid = Convert.ToInt32(ds1.Tables[0].Rows[0]["oid"].ToString());
                for (int i = 1; i <= tblRows; i++)
                {
                    string s = ((DropDownList)Page.FindControl("itemname" + i)).SelectedItem.Text;
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        if (s == ds.Tables[0].Rows[j]["ItemName"].ToString())
                        {
                            arr[i] = ds.Tables[0].Rows[j]["ItemEntry"].ToString();
                        }
                    }
                    int itemno = Convert.ToInt32(arr[i]);
                    int quantity = Convert.ToInt32(((TextBox)Page.FindControl("qty" + i)).Text);
                    //enters the quantity and food item info of the order into the list of items table
                    //with the respective order id
                    string query5 = "INSERT INTO ListOfItems([Orderid], [ItemEntry], [Qty]) VALUES ('" + orderid + "','" + itemno + "','" + quantity + "') ";
                    OleDbCommand comm1 = new OleDbCommand(query5, accessconn);
                    comm1.ExecuteNonQuery();
                }
                accessconn.Close();
            }
            else MessageBox("Please Enter the Quantity");
        }

        //Validate the manager and owner id and password to enter into schedule page
        protected void Button1_Click(object sender, EventArgs e)
        {
            //System.Diagnostics.Debug.Write(hidPassword.Value);
            accessconn.Open();
            DataSet ds2 = new DataSet();
            string query6 = "select Eid, Ename, Password from Employee";
            OleDbDataAdapter da = new OleDbDataAdapter(query6, accessconn);
            da.Fill(ds2);
            bool stat = false;
            for (int r = 0; r < ds2.Tables[0].Rows.Count; r++)
            {
                string eid = ds2.Tables[0].Rows[r]["Eid"].ToString();
                string pass = ds2.Tables[0].Rows[r]["Password"].ToString();
                if (eid.Equals(empid) && pass.Equals(hidPassword.Value) && (empid.StartsWith("M") || empid.StartsWith("O")))
                {
                    stat = true;
                    string ename = ds2.Tables[0].Rows[r]["Ename"].ToString();
                    Response.Redirect("http://localhost:62505/Schedule.aspx?empname=" + ename + "&eid=" + eid);
                }
            }
            if (stat == false)
            {
                MessageBox("Access Restricted");
            }
            accessconn.Close();
        }
        //creates an alert box in javascript 
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        //validate owner info to enter into the owner page
        protected void Button2_Click(object sender, EventArgs e)
        {
            accessconn.Open();
            DataSet ds3 = new DataSet();
            string query6 = "select Eid, Ename, Password from Employee";
            OleDbDataAdapter da = new OleDbDataAdapter(query6, accessconn);
            da.Fill(ds3);
            bool stat = false;
            for (int r = 0; r < ds3.Tables[0].Rows.Count; r++)
            {
                string eid = ds3.Tables[0].Rows[r]["Eid"].ToString();
                string pass = ds3.Tables[0].Rows[r]["Password"].ToString();
                if (eid.Equals(empid) && pass.Equals(hidPassword.Value) && empid.StartsWith("O"))
                {
                    stat = true;
                    string ename = ds3.Tables[0].Rows[r]["Ename"].ToString();
                    Response.Redirect("http://localhost:62505/Owner.aspx?empname=" + ename + "&eid=" + eid);
                }
            }
            if (stat == false)
            {
                MessageBox("Access Restricted");
            }
            accessconn.Close();
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Response.Redirect("http://localhost:62505/ViewSchedule.aspx?empname=" + Label1.Text + "&eid=" + empid);
        }
    }
}
